//https://leetcode.cn/problems/maximum-subarray/
package codeRandomThoughts.Test53最大子序和;

public class Solution {
    public int maxSubArray(int[] nums) {
        int sum = 0;
        int maxSum = nums[0];

        for (int num : nums) {
            if (sum>=0){
                //表示之前的元素有可能参与构建最大子数组
                sum+=num;
            }else {
                //从当前元素开始,重新计算最大和
                sum=num;
            }
            if (maxSum<sum){
                maxSum = sum;
            }
        }

        return maxSum;
    }
}
